package com.zjx.Service.impl;

import com.zjx.Service.StudentService;
import com.zjx.entity.Student;
import com.zjx.mapper.StudentRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.Types;

/**
 * @author zhaojiaxu
 * @date 2021/08/25 10:18
 **/
public class StudentServiceImpl implements StudentService {

	private JdbcTemplate jdbcTemplate;

	public void setDateSource(DataSource dateSource){
		this.jdbcTemplate = new JdbcTemplate(dateSource);
	}
	@Override
	public void save(Student student) throws Exception {
         jdbcTemplate.update("insert into student(name,age,sex) values (?,?,?)",
				 new Object[]{student.getName(),student.getAge(),student.getSex()},new int[]{
				 		Types.VARCHAR,Types.INTEGER,Types.VARCHAR});
	       //事务测试，加上这句代码则数据不会保存到数据库中
		//throw new RuntimeException("测试失败");

		//事务测试，如果抛出Exception会不会回滚呢 结果是不会产生回滚
		throw new Exception("测试失败");
	}
}

